package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 完成员工的添加
     * 需要我们根据接口文档，设计请求内容和响应内容
     */
    @PostMapping
     public Result save(@RequestBody EmployeeDTO employeeDTO) {
                        //@RequestBody 将json格式数据转换成对应的对象
         log.info("添加新的用户"+employeeDTO);
         employeeService.save(employeeDTO);
         return  Result.success();
     }

    /**
     * 完成员工的分页查询(可能根据名字模糊分页查询)
     * @param employeePageQueryDTO
     * @return
     */
     @GetMapping("/page")
     public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
                        //不用添加@RequestBody,三个参数是简单参数
         log.info("员工分页查询参数:{}",employeePageQueryDTO);
         //业务逻辑交给service，需要返回的数据pageResult要给前端
         PageResult pageResult =employeeService.pageQuery(employeePageQueryDTO);
         return Result.success(pageResult);
     }


    /**
     * 完成 员工的启用和禁用
     * @return
     */
    @PostMapping("/status/{status}")
     public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("id为"+id+"的员工,状态修改为:"+status);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 完成 修改的回显 根据id查询 员工基本信息
     *   请求方式 get 请求参数路径参数id
     *   返回的数据 Reuslt<Employee>
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("查询的员工id是:"+id);
        //service来根据id查询一个员工信息
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("修改用户信息，修改后"+employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
